package com.lagou.edu.utils;

import com.lagou.edu.anno.MyAutoWired;
import com.lagou.edu.anno.MyComponent;

import java.sql.SQLException;

/**
 * @author 应癫
 * <p>
 * 事务管理器类：负责手动事务的开启、提交、回滚
 */
public class TransactionManager {

	@MyAutoWired
	private ConnectionUtils connectionUtils;


    /*private TransactionManager(){

    }

    private static TransactionManager transactionManager = new TransactionManager();

    public static TransactionManager getInstance() {
        return  transactionManager;
    }*/


	// 开启手动事务控制
	public void beginTransaction() throws SQLException {
		System.out.println("===============开启事务===============");
		connectionUtils.getCurrentThreadConn().setAutoCommit(false);
	}


	// 提交事务
	public void commit() throws SQLException {
		System.out.println("===============提交事务===============");
		connectionUtils.getCurrentThreadConn().commit();
	}


	// 回滚事务
	public void rollback() throws SQLException {
		System.out.println("===============回滚事务===============");
		connectionUtils.getCurrentThreadConn().rollback();
	}
}
